<?php

// use Illuminate\Support\Facades\Schema;
use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitActivity extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('activities', function (Blueprint $table) {

            $table->comment = '活动表';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->string('title', 255)->nullable()->comment('标题'); // 标题
            $table->string('sub_title', 255)->nullable()->comment('副标题'); // 副标题

            $table->string('thumb', 255)->nullable()->comment('封面'); // 封面
            $table->json('images')->nullable()->comment('详情图');
            $table->string('type', 20)->index()->comment('活动类型 lottery抽奖 seckill秒杀');    

            $table->boolean('is_need_pay')->default(0)->index()->comment('0为免费 1为需要支付'); // none为免费 money为单人民币支付 score为积分支付  money_score

            $table->integer('product_id')->nullable()->index()->comment('参与活动的商品id');
            $table->integer('box_id')->nullable()->index()->comment('参与活动的盲盒id');

            $table->integer('money_price')->index()->nullable()->comment('人民币价格'); // 支付价格
            $table->integer('score_price')->index()->nullable()->comment('积分价格'); // 支付价格

            $table->integer('visit_total')->default(0)->comment('访问人数'); // 访问人数

            $table->integer('prize_total')->default(0)->comment('奖品份数'); // 活动总件数
            $table->integer('stock')->nullable()->comment('剩余库存'); // 剩余库存
            $table->integer('sales')->default(0)->comment('销量'); 

            $table->text('join_codes')->nullable()->comment('参与码'); // 参与码

            $table->timestamp('start_at')->nullable()->index()->comment('开始时间');
            $table->timestamp('end_at')->nullable()->index()->comment('结束时间');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');


 
            // $table->integer('user_total_limit')->nullable()->comment('限制参与人数'); // 限制参与人数

            // $table->boolean('is_emit_end')->default(0)->index()->comment('是否触发活动结束'); // 是否触发活动结束
            // $table->boolean('emit_end_type')->nullable()->index()->comment('触发结束的类型,1为达到结束时间，2为达到人数限制，3为管理员手动结束'); // 触发结束的类型,1为达到结束时间，2为达到人数限制，3为管理员手动结束

            // $table->string('enter_btn_text', 30)->nullable()->comment('按钮文字'); // 按钮文字
            // $table->json('enter_btn_link')->nullable()->comment('按钮跳转链接'); // 按钮文字

            // 抽奖

            // 秒杀
            // $table->integer('product_id')->nullable()->index()->comment('商品id');
            // $table->string('product_uuid', 50)->nullable()->comment('商品uuid');
            // $table->integer('product_money_price')->nullable()->comment('人民币原价');
            // $table->integer('product_score_price')->nullable()->comment('积分原价');

            $table->softDeletes();
            $table->timestamps();
        });


        // 活动奖品
        Schema::create('activity_skus', function (Blueprint $table) {

            $table->comment = '活动sku表';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->integer('activity_id')->index()->nullable()->comment('活动id');

            $table->string('type', 15)->index()->comment('奖品类型, custom自定义 coupon为优惠卡券 card为道具卡  score为积分  redpack为红包  none空奖品'); 
            $table->integer('coupon_id')->index()->nullable()->comment('优惠券id');
            $table->integer('score')->index()->nullable()->comment('积分数量');
            $table->integer('redpack')->index()->nullable()->comment('红包数量');

            $table->integer('product_id')->index()->nullable()->comment('关联商品');
            $table->integer('box_id')->index()->nullable()->comment('关联盲盒');

            $table->string('title', 100)->nullable()->comment('标题'); // 标题
            $table->string('number', 255)->nullable()->comment('编号'); // 副标题
            $table->string('thumb', 255)->nullable()->comment('封面'); // 副标题

            $table->integer('stock')->default(0)->comment('奖品库存'); // 销量
            $table->integer('sales')->default(0)->comment('奖品销量'); // 销量

            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');

            $table->softDeletes();
            $table->timestamps();
        });

        // 秒杀
        Schema::create('activity_seckill', function (Blueprint $table) {
            $table->increments('id');
            $table->uuid('uuid')->index() ;

            // 秒杀
            $table->integer('activity_id')->nullable()->index()->comment('活动id');
            $table->integer('product_id')->nullable()->index()->comment('商品id');
            $table->string('product_uuid', 50)->nullable()->comment('商品uuid');
            $table->integer('product_money_price')->nullable()->comment('人民币原价');
            $table->integer('product_score_price')->nullable()->comment('积分原价');

            $table->softDeletes();
            $table->timestamps();
        });

        // 抽奖
        Schema::create('activity_lottery', function (Blueprint $table) {

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->nullable()->index()->comment('活动id');
            // 秒杀
            $table->string('emit_type', 20)->nullable()->comment('time为按时间, user_total为按人数, ticket_total为按签号数量开奖');

            $table->timestamp('emit_time')->nullable()->index()->comment('触发开奖时间');
            $table->integer('emit_user_total')->nullable()->comment('触发的参与人数');
            $table->integer('emit_ticket_total')->nullable()->comment('触发的签号数量');

            $table->softDeletes();
            $table->timestamps();
        });

        // 抽奖签号
        Schema::create('activity_lottery_ticket', function (Blueprint $table) {

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->string('number', 50)->nullable()->comment('编号');

            $table->integer('activity_id')->index()->comment('活动id');
            $table->integer('activity_record_id')->index()->comment('活动参与记录id');
            $table->integer('user_id')->index()->comment('用户id');

            $table->integer('sku_id')->nullable()->comment('抽中奖品id');
            $table->integer('order_id')->nullable()->comment('订单id');
            $table->boolean('is_picked')->default(0)->index()->comment('0为未领取、1为已领取'); // 是否中奖

            $table->integer('from_type')->default(1)->comment('1参与获得、2助力获得');
            $table->integer('invitee_id')->nullable()->index()->comment('助力者id');
            $table->integer('invite_record_id')->nullable()->index()->comment('助力记录');

            $table->boolean('status')->default(0)->index()->comment('0等待开奖 1已中奖 2未中奖'); // 是否中奖

            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('activities');
        Schema::dropIfExists('activity_skus');

        Schema::dropIfExists('activity_seckill');
        Schema::dropIfExists('activity_lottery');
        Schema::dropIfExists('activity_lottery_ticket');
    }
}
